﻿using System;
using System.Data;
using System.Data.SqlClient;
using System.Collections.Generic;
using System.Text;
using Portal.Environment;
using Portal.Language;
using Portal.Interface;

namespace Portal.CMS
{
    public class UpdateAlbum : WebPart
    {
        public override string WebPartId
        {
            get
            {
                return "UpdateAlbum";
            }
        }

        public override string Title
        {
            get
            {
                return ResourceBLL.GetText(WebPartId,"Title","Cập nhật Album");
            }
        }

        public override string Description
        {
            get
            {
                return ResourceBLL.GetText(WebPartId,"Description","");
            }
        }


        public override string RenderWebPart(LoadParamCls loadParamCls)
        {
            AjaxPro.Utility.RegisterTypeForAjax(typeof(UpdateAlbum));
            string AlbumId = System.Web.HttpContext.Current.Request["AlbumId"];
            AlbumCls AlbumCls = new AlbumBLL().CreateModel(Environment.Environments.ActiveLanguage, AlbumId);
            if (AlbumCls == null)
            {
                return Environment.Environments.GetRecordNotFound();
            }

            string SelectPicture="";
            string Selectbook="";
            if (AlbumCls.Equals("picture")) SelectPicture = " select ";
            if (AlbumCls.Equals("book")) Selectbook = " select ";
            string SelectAlbumTypeText =
              "<select id=drpSelectAlbumType>\r\n" +
              "   <option value=\"\">=======</option>" +
              "   <option value=\"picture\" "+SelectPicture+">Kiểu ảnh</option>" +
              "   <option value=\"book\" "+Selectbook+">Kiểu sách</option>" +
              "</select>\r\n";
            

            string SelectLanguageText =
           "<select id=divSelectLanguage onchange=javascript:DetectUpdateAlbumBody('" + AlbumId + "');>";
            string[] Languages = Environment.Environments.GetLanguages();
            for (int iIndex = 0; iIndex < Languages.Length; iIndex++)
            {
                if (Environment.Environments.ActiveLanguage.Equals(Languages[iIndex]))
                {
                    SelectLanguageText += "<option selected value=\"" + Languages[iIndex] + "\">" + Languages[iIndex] + "</option>";
                }
                else
                {
                    SelectLanguageText += "<option value=\"" + Languages[iIndex] + "\">" + Languages[iIndex] + "</option>";
                }
            }
            SelectLanguageText += "</select>";

            string RetHtml =
                WebPartUtilities.AttachJavascript("Portal.CMS", "Album.js") +
                             Environment.Environments.GetFormDialog(
                             "      <table>" +
                             "          <tr>" +
                             "              <td>" + ResourceBLL.GetText(WebPartId, "UpdateAlbumFormLanguage", "Ngôn ngữ") + "</td>" +
                             "              <td>" + SelectLanguageText + "</td>" +
                             "          </tr>" +
                             "          <tr>" +
                             "              <td>" + ResourceBLL.GetText(WebPartId, "UpdateAlbumCaption", "Album") + "</td>" +
                             "              <td><input class=cssTextBoxDialog id=txtAlbum value=\"" + AlbumCls.Subject+ "\"></td>" +
                             "          </tr>" +

                             //"          <tr>" +
                             //"              <td>" + ResourceBLL.GetText(WebPartId, "Type", "Kiểu") + "</td>" +
                             //"              <td>" + SelectAlbumTypeText + "</td>" +
                             //"          </tr>" +

                             "          <tr>" +
                             "              <td></td>" +
                             "              <td><input type=button onclick=javascript:UploadNewImage('" + AlbumId + "'); value=\"" + ResourceBLL.GetText(WebPartId, "UploadNewImage1", "Upload ảnh đại diện") + "\"><input type=button class=cssSaveButton onclick=javascript:ClientSaveUpdateAlbum('" + AlbumId + "'); value=\"" + ResourceBLL.GetText(WebPartId, "UpdateAlbumSaveButton", "Ghi lại") + "\"><input type=button class=cssCancelButton onclick=javascript:window.close(); value=\"" + ResourceBLL.GetText(WebPartId, "UpdateAlbumCancelButton", "Thoát") + "\"></td>" +
                             "          </tr>" +
                             "      </table>", ResourceBLL.GetText(WebPartId, "UpdateAlbumFormTitle", "Cập nhật Album"));
            return RetHtml;
        }

        [AjaxPro.AjaxMethod]
        public string ServerSideUpdateAlbum(
            string ActiveLanguage,
            string AlbumId,
            string AlbumName,
            string AlbumType)
        {
            try
            {
                Environment.Environments.CheckSessionTimeOut(true);
                if (AlbumName.Trim().Length == 0) throw new Exception(ResourceBLL.GetText(WebPartId,"EnterName","Nhập tên"));
                new AlbumBLL().UpdateAlbum(null, ActiveLanguage, AlbumId, AlbumName, AlbumType);
                return ResourceBLL.GetText(WebPartId,"MessageUpdateOk","Cập nhật thành công!");
            }
            catch (Exception ex)
            {
                return "ERROR: " + ex.Message.ToString();
            }
        }

        [AjaxPro.AjaxMethod]
        public string GetSubject(string ActiveLanguage, string AlbumId)
        {
            try
            {
                Environment.Environments.CheckSessionTimeOut(true);
                AlbumCls AlbumCls = new AlbumBLL().CreateModel(ActiveLanguage, AlbumId);
                return AlbumCls.Subject;
            }
            catch (Exception ex)
            {
                return "ERROR: " + ex.Message.ToString();
            }
        }

        [AjaxPro.AjaxMethod]
        public string GetUploadAvatarUrl(string AlbumId)
        {
            string AppendUrl = Screen.BuildDialogUrl(new ParamCls[]{
                        new ParamCls("WebPartId",new AlbumUploadAvatar().WebPartId),
                        new ParamCls("AlbumId",AlbumId),                        
                    });
            return AppendUrl;
        }

        //[AjaxPro.AjaxMethod]
        //public string ServerSideUploadAvatar(string AlbumId, string UploadFile)
        //{
        //    byte[] b = Environments.GetBytesFromFile(UploadFile);
        //    new AdvImageBLL().UpdateAvatar(null, AdvImageItemId, UploadFile, b);
        //    return ResourceBLL.GetText(WebPartId, "AvatarUploadOk", "Cập nhật ảnh đại diện thành công!");
        //}

    }
}